package liurong.driver.stream;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ReplaceStreamTest {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		// 模板路径
		String templatePath = "D:/jack/02/FMLang.xls";
		Workbook templateWB = null;
		try {
			// 得到excel
			templateWB = Workbook.getWorkbook(new File(templatePath));

			Sheet sheet = templateWB.getSheet(0);
			int rows = sheet.getRows();

			Cell cell = null;
			String enString = null;
			String taiString = null;

			// dom4j加xpath
			String xml = "D:/jack/02/FMLang.xml";
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(xml));
			for (int i = 1; i < rows; i++) {
				cell = sheet.getCell(0, i);
				if (cell.getContents() == null || cell.getContents() == "") {
					continue;
				}
				cell = sheet.getCell(1, i);
				enString = cell.getContents().trim();
				cell = sheet.getCell(2, i);
				taiString = cell.getContents().trim();

				List<Element> enNodeList = (List<Element>) document.selectNodes("//stringid[@en=\"" + enString + "\"]");
				if (enNodeList != null && enNodeList.size() > 0) {
					for (Element enNode : enNodeList) {
						if (enNode != null) {
							Attribute attribute = enNode.attribute("jap");
							if (attribute != null) {
								attribute.setData(taiString);
							}
							attribute = enNode.attribute("lan4");
							if (attribute != null) {
								attribute.setData(taiString);
							}
						}
					}
				}
				List<Element> lan4NodeList = (List<Element>) document
						.selectNodes("//stringid[@lan3=\"" + enString + "\"]");
				if (lan4NodeList != null && lan4NodeList.size() > 0) {
					for (Element lan4Node : lan4NodeList) {
						if (lan4Node != null) {
							Attribute attribute = lan4Node.attribute("jap");
							if (attribute != null) {
								attribute.setData(taiString);
							}
							attribute = lan4Node.attribute("lan4");
							if (attribute != null) {
								attribute.setData(taiString);
							}
						}
					}
				}
			}

			// 指定文件输出的位置
			FileOutputStream out = new FileOutputStream("D:/jack/02/res/final/FMLang.xml");
			// 1.创建写出对象
			XMLWriter writer = new XMLWriter(out);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
